<template>
    <div class="my-container">
      <div class="header">
    <!-- 已登录 -->
    <div v-if="user" class="user-info">
      <div class="base-info">
        <div class="login-btn">
          <img class="mobile-img" :src="`http://122.51.249.55:8080${userInfo.avatar}`" />
          <span class="infoName">{{userInfo.nickname}}</span>
          <van-button class="loginBtn" type="primary" size="small" @click="onlogout()">退出</van-button>
          <span class="infoName">编辑个人资料 ></span>
        </div>
      </div>
    </div>
    <!-- 未登录 -->
    <div v-else class=" not-login">
      <div class="login-btn" @click="$router.push('/login')">
        <img class="mobile-img" src="~@/assets/images/avatar.png" />
        <span class="infoName">游客</span>
        <van-button class="loginBtn" type="primary" size="small" >去登录</van-button>
      </div>
    </div>
      </div>
    <!-- 宫格导航 -->
    <van-grid class="grid-nav mb-9" :column-num="3" clickable>
      <van-grid-item class="grid-item" @click="$router.push('/collect')">
        <template #icon>
          <i class="iconfont icon-coll"></i>
        </template>
        <template #text>
          <span class="text">我的收藏</span>
        </template>
      </van-grid-item>
      <van-grid-item class="grid-item" @click="$router.push('/rent')">
        <template #icon>
          <i class="iconfont icon-ind"></i>
        </template>
        <template #text>
          <span class="text">我的出租</span>
        </template>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <template #icon>
          <i class="iconfont icon-record"></i>
        </template>
        <template #text>
          <span class="text">看房记录</span>
        </template>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <template #icon>
          <i class="iconfont icon-identity"></i>
        </template>
        <template #text>
          <span class="text">成为房主</span>
        </template>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <template #icon>
          <i class="iconfont icon-myinfo"></i>
        </template>
        <template #text>
          <span class="text">个人资料</span>
        </template>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <template #icon>
          <i class="iconfont icon-cust"></i>
        </template>
        <template #text>
          <span class="text">联系我们</span>
        </template>
      </van-grid-item>
    </van-grid>
    <!-- /宫格导航 -->
    <!-- 底部图片 -->
    <div class="profile_ad">
      <img src="~@/assets/images/join.png" alt="">
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getUserInfo } from "@/api/user"; //导入获取用户方法
export default {
  name: "profile",
  components: {},
  props: {},
  data() {
    return {
      userInfo: {}
    };
  },
  computed: {
    ...mapState(["user"])
  },
  created() {
    if (this.user) {
      this.loadUserInfo();
    }
  },
  mounted() {},
  methods: {
  async loadUserInfo() {
    try {
    const {body}  = await  getUserInfo()
    console.log(body);
    this.userInfo = body
    } catch (error) {
      console.log(error);
    }
    },
      onlogout() {
      this.$dialog
        .confirm({
          title: "确认退出吗？"
        })
        .then(() => {
          this.$store.commit("setUser", null);
        })
        .catch(() => {
          console.log("取消执行");
        });
    },
    
  },
  watch: {},
};
</script>
<style scoped lang="less">
.my-container {
  height: 361px;
  position: relative;
  background: url("~@/assets/images/bg.png") no-repeat;
  background-size: cover;
  .header {
    position: absolute;
    height: 361px;
    top: 300px;
    width: 80%;
    left: 50%;
    transform: translateX(-50%);
   display: flex;
    justify-content: center;
    align-items: center;
    background-color: #fff;
        box-shadow: 0 0 0.13333rem 0.04rem #ddd;
  }
  .not-login {
    position: absolute;
    transform: translateY(-50%);
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .mobile-img {
        width: 132px;
        height: 132px;
        border-radius: 50%;
      }
      .infoName {
        font-size: 30px;
      }
      .loginBtn {
        margin-top: 10px;
      }
      .text {
        font-size: 28px;
        color: #fff;
      }
    }
  }
   .user-info {
    position: absolute;
    transform: translateY(-30%);
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .mobile-img {
        width: 132px;
        height: 132px;
        border-radius: 50%;
      }
      .infoName {
        font-size: 20px;
        margin-top: 30px;
      }
      .loginBtn {
        margin-top: 15px;
      }
      .text {
        font-size: 28px;
        color: #fff;
      }
    }
  }
  .grid-nav {
    position: absolute;
    top: 700px;
    width: 100%;
    .grid-item {
      height: 141px;
      .iconfont {
        font-size: 45px;
      }
      .toutiao-shoucang {
        color: #eb5253;
      }
      .toutiao-lishi {
        color: #ff9d1d;
      }
      span.text {
        font-size: 28px;
      }
    }
  }
  .mb-9 {
    margin-bottom: 9px;
  }
  .profile_ad img{
    width: 100%;
    position: fixed;
    bottom: 120px;
  }
}
</style>
